import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;

import org.springframework.util.ObjectUtils;

import com.alibaba.fastjson2.JSON;
import com.lyons.generate.util.FileUtils;

public class MenuJsonToJavaBean {

	public static void main(String[] args) throws IOException {

		String path = "D:/workspackes/java/xy_payment/src/main/resources/static/menu/system.json";

		String json = FileUtils.readFileToString(path, false);

		JsonRootBean bean = JSON.parseObject(json, JsonRootBean.class);

		List<MenuInfo> list = bean.getMenuInfo();
		for (MenuInfo menu : list) {

			List<Child> childList = menu.getChild();

			for (Child child : childList) {

				if (!child.getTitle().contains("控制台")) {

					if (ObjectUtils.isEmpty(child.getChild())) {
						if (!child.getTitle().contains("接口文档") && !child.getTitle().contains("数据监控") && !child.getHref().equals("/druid") && !child.getHref().equals("/doc.html")) {
							System.out.println(child.getTitle() + "------" + child.getHref());
							createMenthod(child);
						}
					} else { // 按照模块划分
						for (Child c : child.getChild()) {
							if (!c.getTitle().contains("接口文档") && !c.getTitle().contains("数据监控")) {
//								System.out.println(c.getTitle() + "------" + c.getHref());
								createMenthod(c);
							}
						}
					}
				}
			}
		}
	}

	public static void createMenthod(Child child) {

		String href = child.getHref();

		String[] name = href.split("/");

		String clsnName = name[2] + name[3].substring(0, 1).toUpperCase() + name[3].substring(1);

		String ret = "/system/" + name[2] + "/" + name[3];

		StringBuffer sb = new StringBuffer();
		sb.append("@GetMapping(\"").append(href).append("\")\n");
		sb.append("public String ").append(clsnName).append("Index(){\n\n");
//		sb.append("\tlog.info(\"").append(child.getTitle()).append(" - 管理....\");\n");

		sb.append("\treturn \"").append(ret).append("/index.html").append("\";\n}\n\n");

		System.out.println(sb.toString());
		saveMenthod(sb.toString());

		sb = new StringBuffer();

		href = href.substring(0, href.lastIndexOf("/") + 1);
		sb.append("@GetMapping(\"").append(href).append("form.html").append("\")\n");
		sb.append("public String ").append(clsnName).append("Form(){\n\n");
//		sb.append("\tlog.info(\"").append(child.getTitle()).append(" - 编辑....\");\n");
		sb.append("\treturn \"").append(ret).append("/form.html").append("\";\n}\n\n");

		System.out.println(sb.toString());

		saveMenthod(sb.toString());
	}

	private static void saveMenthod(String menthodStr) {

		File file = new File("D:/PageController.java");

		try {

			FileWriter fw = new FileWriter(file, true);
			fw.write(menthodStr);

			fw.close();

		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
